'use strict';

/**
 * @ngdoc function
 * @name sugarfreeMemberFrontendApp.controller:AboutCtrl
 * @description
 * # AboutCtrl
 * Controller of the sugarfreeMemberFrontendApp
 */
angular.module('sugarfreeMemberFrontendApp')
.controller('MineCtrl', ['$uibModal', '$state', 'toaster', '$http', 'OauthService', 'status',
		function ($uibModal, $state, toaster, $http, OauthService, status) {

	var self = this;

	self.alerts = [];

	self.status = status;

	console.log(OauthService.getToken());

	self.openChargeModal = function(){

		var modal = $uibModal.open({
			templateUrl:'charge.html',
			controller:'ChargeCtrl',
			size:'sm'
		});

		modal.result.then(function(beans){

			var subject = beans+'Bean';
			 var url = '/member/order/create?channel=wx_pub&beans=' + beans + '&subject=' + subject + 
				 '&deviceType=2&openId='+OauthService.getToken().openid;

			 OauthService.getWithSession(url).success(function(charge){
				self.alerts.push(charge);
				pingpp.createPayment(charge, function(result, error){

					self.alerts.push(result);
					self.alerts.push($state.current);

					if(result==='success'){

						$state.reload();
						/*
						$state.go('main.mine');
						var modal = $uibModal.open({
							templateUrl:'confirm.html',
							controller: function($scope, $uibModalInstance){
								$scope.close = function(){
									$uibModalInstance.close();
								};
							},
							size:'sm'
						});

						modal.result.then(function(){
							toaster.pop('success', '充值成功' , beans);
						});
						 */
					} else if (result === 'fail'){
						console.log('fail');
					} else if (result === 'cancel'){
						console.log('cancel');
					} else {
						console.log('unknow');
					}
			
				});
			});
		
		});
	
	};

	self.openRedeemModal = function(){

		var modal = $uibModal.open({
			templateUrl:'redeem.html',
			controller:'RedeemCtrl',
			size:'sm'
		});

		modal.result.then(function(code){

			OauthService.getWithSession('/member/exchangeCode/verify?code=' + code)
				.success(function(resp){
					if('8000'===resp.errCode){
						$state.reload();
						//toaster.pop('success', '兑换成功' ,$state.current);
					} else {
						toaster.pop('error', '兑换失败' ,resp.errMsg);
					}
				});
			/*
			 */
		
		});
	
	};

	self.alerts.push( OauthService.getToken() );
	self.alerts.push( OauthService.getSession() );

	if(!!OauthService.getSession()){
		 OauthService.getWithSession('/member/get?').success(function(resp){
			 self.user = resp.data;
			 self.alerts.push( resp.data );
		});

		OauthService.getWithSession('/member/order/query?')
			.success(function(resp){
				
				self.orders = resp.data;
			});

		OauthService.getWithSession('/member/receipt/queryPackageLessonReceipts?')
			.success(function(resp){
				
				self.courseWrappers = resp.data;
			});

		OauthService.getWithSession('/member/receipt/query?status=1&classType=2')
			.success(function(resp){

				self.lessonWrappers = resp.data;
			});

		OauthService.getWithSession('/member/receipt/query?status=1&classType=3')
			.success(function(resp){

				self.ptWrappers = resp.data;
			});

	} else {
		//$state.go()
		OauthService.login('mineS'+0);
	}

}])
.controller('RedeemCtrl', function($scope, $uibModalInstance){
	
	$scope.redeem = function(code){
		console.log(code);
		$uibModalInstance.close(code);
	};

})
.controller('ChargeCtrl', function($scope, $uibModalInstance){
	
	$scope.charge = function(beans){
		console.log(beans);
		$uibModalInstance.close(beans);
	};

});
